package com.xiayuanxing.arithmetic.DataStructures.Queue;

import java.util.Scanner;

/**
 * @program: arithmetic
 * @description: 数组模拟队列
 * @author: xia yuan xing
 * @create: 2021-08-31 10:15
 */
public class ArrayQueueDeme {

    public static void main(String[] args) {
        //测试
        ArrayQueue arrayQueue = new ArrayQueue(3);

        //接收用户输入
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        //输出一个菜单
        while (loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出队列");
            System.out.println("a(add): 添加队列");
            System.out.println("g(get): 获取队列中的数据");
            System.out.println("h(head): 查看队列头部数据");
            //接收用户输入的字符串
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数字：");
                    int num = scanner.nextInt();
                    arrayQueue.addQueue(num);
                    break;
                case 'g':
                    try {
                        int n = arrayQueue.getQueue();
                        System.out.printf("取出的数据是%d\n", n);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int n = arrayQueue.headQueue();
                        System.out.printf("取出的头部数据是%d\n", n);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出！");
    }
}

//使用数组模拟一个队列 -编写一个ArrayQueue
class ArrayQueue {
    //表示数组的最大容量
    private int maxSize;
    private int front;
    private int rear;//表示队列尾
    private int[] arr;//该数组用于存放数据，模拟队列


    public ArrayQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        front = -1;//指向队列头部，不包含队列中第一个数据
        rear = -1;//指向队列尾部，包含队列尾部最后一个数据
    }

    //判断队列是否满？
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    //判断队列是否为空
    public boolean isNull() {
        return rear == front;
    }

    //添加数据到队列
    public void addQueue(int num) {
        //判队列是否满？
        if (isFull()) {
            System.out.println("队列已经满了");
            return;
        }
        rear++;//让rear后移
        arr[rear] = num;
    }

    //获取队列数据
    public int getQueue() {
        if (isNull()) {
            throw new RuntimeException("队列为空，无数据可取出");
        }
        front++;//让front后移
        return arr[front];
    }

    //显示队列的所有数据
    public void showQueue() {
        if (isNull()) {
            System.out.println("队列为空");
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d]=%d\n", i, arr[i]);
        }
    }

    //显示队列头数据
    public int headQueue() {
        //判断队列是否为空
        if (isNull()) {
            throw new RuntimeException("队列为空");
        }
        return arr[front + 1];
    }
}